#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define QUEUE_LEN 10


//先进先出
typedef struct Queue
{
	int arr[QUEUE_LEN];
	int head;//队列头
	int tail;//队列尾
}Queue;
//入队
void push(Queue *queue, int data)
{
	if (isFull(queue))
	{
		printf("入队失败,队满...\n");
		return;
	}
	queue->arr[queue->tail] = data;
	queue->tail = (queue->tail+1)%QUEUE_LEN;
}

//出栈:将最后进去的数据扔掉
void pop(Queue *queue)
{
	if (isEmpty(queue))
	{
		printf("出队失败,队空...\n");
		return;
	}
	queue->arr[queue->head] = 0;
	queue->head = (queue->head+1)%QUEUE_LEN;
}

//获得队头元素的值
int getTop(Queue *queue)
{
	if (isEmpty(queue))
	{
		printf("获得队头元素失败,队空...\n");
		return;
	}
	return queue->arr[queue->head];
}

//判断队列是否满了
int isFull(Queue *queue)
{	
	int ret = 0;
	if (NULL != queue)
	{
		if (queue->head == (queue->tail+1)%QUEUE_LEN)
		{
			ret = 1;
		}
	}
	return ret;
}

//判断队列是否空了
int isEmpty(Queue *queue)
{
	int ret = 0;
	if (NULL != queue)
	{
		if (queue->head == queue->tail)
		{
			ret = 1;
		}
	}
	return ret;
}

int main(void)
{
	Queue queue;
	memset(&queue, 0, sizeof(queue));
	
	int iNum = 0;
	while (1)
	{
		printf("请输入入队数据:\n");
		scanf("%d", &iNum);
		push(&queue, iNum);
		if (isFull(&queue))
		{
			break;
		}
	}

	while (1)
	{
		if (isEmpty(&queue))
		{
			break;
		}
		printf("data=%d\n", getTop(&queue));
		pop(&queue);
	}

	return 0;
}
